{extend name="../../../view/public/base" /}

{block name="css"}
<style>
    .xm-body {
        height: 400px !important;
        overflow-y: scroll !important;
    }
    .layui-form-selected dl {
        height: 200px !important;
        overflow-y: scroll !important;
    }
</style>
<link rel="stylesheet" href="/layui/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
{/block}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">

            <!--搜索区域-->
            <!--<div class="layui-card">-->
            <!--<div class="layui-collapse" lay-accordion="" lay-filter="search">-->
            <!--<div class="layui-colla-item">-->
            <!--<h2 class="layui-colla-title">查询条件</h2>-->
            <!--<div class="layui-colla-content">-->
            <!--<form id="searchForm" class="layui-form" lay-filter="searchForm" autocomplete="off">-->

            <!--<div class="layui-form-item layui-inline">-->
            <!--<label class="layui-form-label">是否显示</label>-->
            <!--<div class="layui-input-inline">-->
            <!--<select name="is_show" id="show">-->
            <!--<option value="">请选择</option>-->

            <!--<option value="1" >显示</option>-->

            <!--<option value="0" >隐藏</option>-->

            <!--</select>-->
            <!--</div>-->
            <!--</div>-->

            <!--&lt;!&ndash;分类id&ndash;&gt;-->
            <!--<div class="layui-form-item layui-inline">-->
            <!--<label class="layui-form-label">商品分类</label>-->
            <!--<div class="layui-input-inline" lay-verify='cate_id'>-->
            <!--<div id="demo1"  class="xm-select-demo" style="margin: 0 5px 0 0 ;"></div>-->
            <!--<input name="cate_id" id="cate_id" type="hidden" >-->
            <!--</div>-->
            <!--</div>-->
            <!--<div class="layui-form-item layui-inline">-->
            <!--<label class="layui-form-label">分类名称</label>-->
            <!--<div class="layui-input-inline">-->
            <!--<input type="text" name="cate_name" id = 'edtSearch' placeholder="查询分类名称"-->
            <!--class="layui-input">-->
            <!--</div>-->
            <!--</div>-->

            <!--<div class="layui-form-item">-->
            <!--<div class="layui-input-block">-->
            <!--<button class="layui-btn layui-btn-normal" type="button" id="btnSearch" lay-submit lay-filter="searchBtn">-->
            <!--搜索-->
            <!--</button>-->
            <!--<button class="layui-btn layui-btn-primary" type="button" onclick="resetForm()">-->
            <!--重置-->
            <!--</button>-->
            <!--</div>-->
            <!--</div>-->
            <!--</form>-->
            <!--</div>-->
            <!--</div>-->
            <!--</div>-->
            <!--</div>-->

            <!--列表区域-->
            <!--<div class="layui-card" style="background-color: #F2F2F2;">-->
            <!--<table id="list" lay-filter="listFilter"></table>-->

        </div>

        <div class="layui-card">
            <div>
                <div class="layui-btn-group">
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn" id="btn-fold">全部折叠</button>
                </div>
                <table  class="layui-table" id="list" lay-filter="listFilter"></table>
                <!--工具栏-->
                <script type="text/html" id="listToolbar">
                    <a class="layui-btn layui-btn-sm layui-bg-black" lay-event="refresh" title="刷新"><i class="layui-icon layui-icon-refresh" style="font-size: 8px !important;"></i> </a>
                    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" title="添加分类">添加角色组</a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除角色组</a>
                </script>
                <!--父id-->
                <script type="text/html" id="pid">
                    {{ d.pid }}
                </script>
                <!--分类名称-->
                <script type="text/html" id="cate_name">
                    {{ d.cate_name }}
                </script>
                <!--排序-->
                <script type="text/html" id="sort">
                    {{ d.sort }}
                </script>
                <script type="text/html" id="status">
                    <input type="checkbox"  name="status" value="{{d.id}}"  lay-skin="switch" lay-text="有效|无效" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
                </script>
                <script type="text/html" id="name11">
                    {{#  if(d.member_id == 0&&d.scopes == 'member'){ }}
                    <span class="layui-badge">系统应用功能模块组</span>
                    {{#  } else { }}
                    <span class="layui-badge layui-bg-gray">角色组</span>
                    {{#  } }}
                </script>
                <!--添加时间-->
                <script type="text/html" id="add_time">
                    {{ formatDate(d.add_time) }}
                </script>
                <!--状态开关-->
                <script type="text/html" id="listBar">
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="add_child" title="添加子角色组">添加子角色组</a>
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
</div>

{/block}

{block name="js"}
<script>
    var element, layer, form, table, bwajax, manage,xmSelect,bwajax,$,eleTree,treetable;

    layui.use(['element', 'layer', 'form', 'table', 'bwajax', 'manage', 'xmSelect','bwajax','eleTree','treetable'], function () {
        element = layui.element;
        layer = layui.layer;
        form = layui.form;
        table = layui.table;
        manage = layui.manage;
        xmSelect = layui.xmSelect;
        bwajax = layui.bwajax.instance();
        eleTree = layui.eleTree;
        treetable = layui.treetable;
        $ = layui.jquery;


        var isClick = true;
        // form.on('switch(state)', function(obj){
        //     tipText = "操作成功";
        //     var emelment = $('.layui-form-switch');
        //
        //     if(isClick){
        //         isClick = false;
        //
        //         bwajax.post("{:Url('setShow')}",{id:this.value})
        //             .then(function (response) {
        //                 if (response.data.data.errcode === 0) {
        //                     layer.msg(tipText, {icon: 1});
        //                 } else {
        //                     layer.msg(response.data.msg, {icon: 2});
        //                 }
        //
        //                 // for (var i = 0; i < emelment.length; i++) {
        //                 //     $(emelment[i]).addClass('layui-disabled');
        //                 //     $(emelment[i]).unbind();
        //                 // }
        //                 // setTimeout(function(){
        //                 isClick = true;
        //                 //     curr = $('.layui-laypage-em').next().text();
        //                 //     location.reload();
        //                 // },1500);
        //             });
        //     }else{
        //         layer.msg('请不要操作太频繁！', {
        //             icon: 5,
        //             time: 1000
        //         });
        //     }
        // });

        form.on('switch(status)', function(obj){
            tipText = "操作成功";
            var emelment = $('.layui-form-switch');

            if(isClick){
                isClick = false;

                bwajax.post("{:Url('setStatus')}",{id:this.value})
                    .then(function (response) {
                        if (response.data.data.errcode === 0) {
                            layer.msg(tipText, {icon: 1});
                        } else {
                            layer.msg(response.data.msg, {icon: 2});
                        }

                        // for (var i = 0; i < emelment.length; i++) {
                        //     $(emelment[i]).addClass('layui-disabled');
                        //     $(emelment[i]).unbind();
                        // }
                        // setTimeout(function(){
                        isClick = true;
                        //     curr = $('.layui-laypage-em').next().text();
                        //     location.reload();
                        // },1500);
                    });
            }else{
                layer.msg('请不要操作太频繁！', {
                    icon: 5,
                    time: 1000
                });
            }
        });



        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            toolbar: '#listToolbar',
            treeIdName: 'id',
            treeSpid: 0,//最上级的父级id
            treePidName: 'pid',
            treeDefaultClose: true,//是否默认折叠
            elem: '#list',
            url: "{:Url('index')}",//"/layui/api/menus.json",
            page: false,
            cols: [[
                {type:'checkbox', fixed: 'left'},
                {field: 'name', minWidth: 200, title: '角色组名'},
                {field: 'name11', title: '类型' ,templet: '#name11'},
                {field: 'status', title: '是否有效' ,templet: '#status'},
                {field: 'group_name', minWidth: 200, title: '唯一标识'},
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 250}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#list');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#list');
        });

        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.msg('删除' + data.id);
            } else if (layEvent === 'edit') {
                layer.msg('修改' + data.id);
            }
        });




        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;

                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');

                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);

                    break;
            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('edit')}?id=" + data.id,
                    });
                    break;
                case 'del':
                    del(obj.data.id);
                    break;
                case 'add_child':
                    layer.open({
                        title: '新增子角色组',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('add')}?pid="+data.id,
                    });
                    break;


            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);

            return false;
        })


    });


    // function getWhere(){
    //     console.log(111111);
    //    var show =  $('#show').val();
    //    var cate_id =  $('#cate_id').val();
    //    var cate_names =  $('#cate_names').val();
    //    return {
    //        is_show: show,
    //        cate_id: cate_id,
    //        cate_name: cate_names
    //    };
    // }


    /**
     * 返回一组图片标签
     */
    function getImgs(json_data,type) {
        console.log(json_data);
        var rs = '';
        //单图
        if(type==1){
            //得到图片
            rs =  '<img src="'+json_data+'" style="border-radius:50%" class="layui-upload-img" width="25" height="25" onclick="openimgs(this)">';
        }
        //多图
        if(type==2){
            for (img in json_data) {
                rs += '<img src="'+json_data[img]+'" style="border-radius:50%" class="layui-upload-img" width="25" height="25" onclick="openimgs(this)">&nbsp&nbsp';
            }

        }
        //单文件
        if(type==3){

            rs +=  '<span class="layui-badge layui-bg-green">文件名：'+json_data.name+'</span>';
            rs +=  '<span class="layui-badge layui-bg-blue">大小：'+(json_data.size/1024)+'kb</span>';
        }

        return rs;
    }


    //表格重载
    function reload(page) {
        setTimeout(function () {
            location.reload();
        }, 500);

    }


    //删除
    function del(ids) {
        layer.confirm('确认删除?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('delete')}?ids=" + ids)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('删除成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

</script>
{/block}